package com.atguigu.lease.web.admin.controller.apartment;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.web.admin.service.FileService;
import io.minio.errors.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;


@Tag(name = "文件管理")
@RequestMapping("/admin/file")
@RestController
public class FileUploadController {

    @Autowired
    FileService fileService;

    /**
     * 文件上传要求：
     *      1.必须post请求
     *      2.表单域类型<type="file" name="file"></>   一般前端框架文件上传参数名称： "file"
     *      3.文件上传的内容类型   <form formenctype="multipart/form-data"  action="/admin/file/upload"></form>
     * @param file   SpringMVC框架用MultipartFile接收文件。
     * @return
     */
    @Operation(summary = "上传文件")
    @PostMapping("upload")
    public Result<String> upload(@RequestParam MultipartFile file) throws Exception{
        String url =  fileService.upload(file);
        return Result.ok(url);
        //下面代码属于重复性代码，需要统一解决：    全局异常处理器
       /* try {
            String url =  fileService.upload(file);
            return Result.ok(url);
        } catch (Exception e) {
            return Result.fail();
        }*/
    }

}
